$.pjax({
    selector: '[data-pjax]',
    container: '#content', //内容替换的容器
    //show: 'fade',  //展现的动画，支持默认和fade, 可以自定义动画方式，这里为自定义的function即可。
    cache: false,  //是否使用缓存
    storage: false,  //是否使用本地存储
    //titleSuffix: '-文章', //标题后缀
});
$(function() {
	//手机端网页禁用一些元素
	mobile();
    // 获取头部导航栏
    getNav();
    //音乐盒
    getMusic();
    // 获取近期文章
    getRecentArticles();
    // 获取分类目录
    getSideCategory();
    // 获取标签
    getTags();
    // 获取文章归档
    getArticleArchives();
    
});
$('.navbar-brand').on("click",function(){
	getArticleList(1);
	return false;
});

/**
 * 判断该分类下是否有子分类，有则返回true,没有返回false
 */
function hasSecound(list, id) {
    for (var i = 0; i < list.length; i++) {
        if (list[i].pid == id) {
            return true;
        }
    }
    return false;
}
/**
 * 文章列表
 */
function getArticleList(pageNumber) {
    $.ajax({
        url: "/article/getArticleList",
        data: {
            pageNumber: pageNumber
        },
        type: "post",
        success: function(page) {
            var list = page.list;
            var content = "<h3>主页 / 文章</h3>";
            for (var i = 0; i < list.length; i++) {
                content += "<div class='ibox'><div class='ibox-content'><a data-pjax href='/article?id="+list[i].id+"' class='btn-link'><h2>";
                content += list[i].title;
                content += "</h2></a><div class='small m-b-xs'>";
                content += list[i].author;
                content += "<span class='text-muted'> <i class='fa fa-clock-o'></i>&nbsp;";
                content += list[i].createTime;
                content += "</span></div><p>";
                content += list[i].abstractContent;
                content += "</p><div class='row'><div class='col-md-6'>";
                var tags = list[i].tags;
                if (tags != "") {
                    content += "<i class='fa fa-tags'></i>&nbsp;标签：&nbsp;";
                    for (var j = 0; j < tags.length; j++) {
                        content += "<button class='btn btn-white btn-xs' type='button'>";
                        content += tags[j].tagName;
                        content += "</button>&nbsp;";
                    }
                }
                content += "</div><div class='col-md-6 text-right'><i class='fa fa-bar-chart'></i>&nbsp;状态：&nbsp;<i class='fa fa-comments-o'></i>&nbsp;";
                content += list[i].commentCount;
                content += "&nbsp;评论&nbsp;<i class='fa fa-eye'></i>&nbsp;";
                content += list[i].visitorCount;
                content += "&nbsp;浏览</div></div></div></div>";
            }
            content += getArticlePagination(page.pageNumber, page.totalPage);
            $("#content").html(content);
        }
    });
}
/**
 * 返回首页的分页html代码
 */
function getArticlePagination(pageNumber, totalPage) {
    if (totalPage == 1) {
        return "";
    }
    var pagination = "<ul class='pagination pagination-lg' style='margin:auto;width:auto;display:table;'>";
    if (pageNumber != 1) {
        pagination += "<li><a href='javascript:void(0)' onclick='getArticleList(" + (pageNumber - 1) + ")'>&laquo;</a></li>";
    }
    if (totalPage < 9 || (totalPage >= 9 && pageNumber < 5)) {
        pagination += getPageli(1, totalPage + 1, pageNumber);
    } else {
        if (pageNumber + 5 > totalPage) {
            pagination += getPageli(totalPage - 7, totalPage + 1, pageNumber);
        } else {
            pagination += getPageli(pageNumber - 3, pageNumber + 5, pageNumber);
        }
    }
    if (pageNumber != totalPage) {
        pagination += "<li><a href='javascript:void(0)' onclick='getArticleList(" + (pageNumber + 1) + ")'>&raquo;</a></li>";
    }
    pagination += "</ul>";
    return pagination;
}
/**
 * 返回分页的页码部分html代码
 */
function getPageli(start, end, pageNumber) {
    for (var pagination = "", i = start; i < end; i++) {
        pagination += "<li";
        if (i == pageNumber) {
            pagination += " class='active'";
        }
        pagination += "><a href='javascript:void(0)' onclick='getArticleList(" + i + ")'>" + i + "</a></li>";
    }
    return pagination;
}
/**
 * 头部导航
 */
function getNav() {
    $.ajax({
        url: "/category/getNav",
        type: "post",
        success: function(list) {
            var nav = "<ul class='nav navbar-nav'>";
            for (var i = 0; i < list.length; i++) {
                if (list[i].pid == 0) {
                    if (hasSecound(list, list[i].id)) {
                        nav += "<li class='dropdown'><a href='#' class='dropdown-toggle' data-toggle='dropdown'> ";
                        nav += list[i].name;
                        nav += " <b class='caret'></b></a>";
                        nav += "<ul class='dropdown-menu'>";
                        for (var j = 0; j < list.length; j++) {
                            if (list[j].pid == list[i].id) {
                                nav += "<li><a href='#'>" + list[j].name + "</a></li>";
                            }
                        }
                        nav += "</ul></li>";
                    } else {
                        nav += "<li><a href='#'>" + list[i].name + "</a></li>";
                    }
                }
            }
            nav += "</ul>";
            $("#nav").html(nav);
        }
    });
}
/**
 * 近期文章
 */
function getRecentArticles() {
    $.ajax({
        url: "/article/getRecentArticles",
        type: "post",
        async: false,
        success: function(list) {
            var recent = "<div class='ibox float-e-margins'><div class='ibox-title'>" + "<h5>近期文章</h5></div>" + "<div class='ibox-content no-padding'>" + "<ul class='list-group'>";
            for (var i = 0; i < list.length; i++) {
                recent += "<li class='list-group-item'><span class='badge";
                if (list[i].count > 20) {
                    recent += " badge-danger";
                } else if (list[i].count > 10) {
                    recent += " badge-success";
                } else if (list[i].count > 5) {
                    recent += " badge-primary";
                } else {
                    recent += " badge-info";
                }
                recent += "'>" + list[i].count + "</span><a href='javascript:void(0)' onclick='getArticle("+list[i].id+")' class='btn-link'>" + list[i].title + "</a></li>";
            }
            recent += "</ul></div></div>";
            $("#sidebar").append(recent);
        }
    });
}
/**
 * 标签
 */
function getTags() {
    $.ajax({
        url: "/tag/getTags",
        type: "post",
        async: false,
        success: function(list) {
            var tags = "<div class='ibox float-e-margins'>" + "<div class='ibox-title'>" + "<h5>标签云</h5>" + "</div><div class='ibox-content' style='overflow: hidden; padding-bottom: 10px;'>";
            for (var i = 0; i < list.length; i++) {
                tags += "<a href=''><span class='label";
                if (list[i].count > 20) {
                    tags += " label-danger";
                } else if (list[i].count > 10) {
                    tags += " label-success";
                } else if (list[i].count > 5) {
                    tags += " label-primary";
                } else {
                    tags += " label-info";
                }
                tags += " tags'>" + list[i].name + "</span></a>";
            }
            tags += "</div></div>";
            $("#sidebar").append(tags);
        }
    });
}
/**
 * 分类目录
 */
function getSideCategory() {
    $.ajax({
        url: "/category/getSideCategory",
        type: "post",
        async: false,
        success: function(list) {
            var sideCategory = "<div class='ibox float-e-margins'><div class='ibox-title'>" + "<h5>分类目录</h5></div><div class='ibox-content no-padding'><ul class='list-group'>";
            for (var i = 0; i < list.length; i++) {
                sideCategory += "<li class='list-group-item'><span class='badge"
                if (list[i].count > 20) {
                    sideCategory += " badge-danger";
                } else if (list[i].count > 10) {
                    sideCategory += " badge-success";
                } else if (list[i].count > 5) {
                    sideCategory += " badge-primary";
                } else {
                    sideCategory += " badge-info";
                }
                sideCategory += "'>";
                sideCategory += list[i].count;
                sideCategory += "</span><a href='javascript:void(0)' onclick='getArticle("+list[i].id+")' class='btn-link'>";
                sideCategory += list[i].name;
                sideCategory += "</a></li>";
            }
            sideCategory += "</ul></div></div>";
            $("#sidebar").append(sideCategory);
        }
    });
}
/**
 * 文章归档
 */
function getArticleArchives() {
    $.ajax({
        url: "/article/getArticleArchives",
        type: "post",
        async: false,
        success: function(list) {
            var archives = "<div class='ibox float-e-margins'><div class='ibox-title'><h5>文章归档</h5></div>";
            archives += "<div class='ibox-content no-padding'><ul class='list-group'>";
            for (var i = 0; i < list.length; i++) {
                archives += "<li class='list-group-item'><span class='badge";
                if (list[i].count > 20) {
                    archives += " badge-danger";
                } else if (list[i].count > 10) {
                    archives += " badge-success";
                } else if (list[i].count > 5) {
                    archives += " badge-primary";
                } else {
                    archives += " badge-info";
                }
                archives += "'>";
                archives += list[i].count;
                archives += "</span><a href='' class='btn-link' year='" + list[i].year + "' month='" + list[i].month + "'>";
                archives += list[i].content;
                archives += "</a></li>";
            }
            archives += "</ul></div></div>";
            $("#sidebar").append(archives);
        }
    });
}
/**
 * 首页点击文章标题获取文章
 * @param articleId
 */
function getArticle(articleId) {
    $.ajax({
        url: "/article/getArticle",
        data: {
            articleId: articleId
        },
        type: "post",
        success: function(map) {
        	var article="<div class='ibox'><div class='ibox-content'><div class='pull-right'>";
        	var tags=map.tags;
        	for (var i = 0; i < tags.length; i++) {
				article+="<button class='btn btn-white btn-xs' type='button'>"+tags[i].tagName+"</button>&nbsp;";
			}
        	article+="</div><div class='text-center article-title' style='margin: 40px 0 25px;'><h1>";
        	article+=map.title;
        	article+="</h1></div><div class='markdown-body editormd-html-preview'>";
        	article+=map.htmlContent;
        	article+="</div><span class='pull-right text-muted'><i class='fa fa-clock-o'></i>&nbsp;";
        	article+=map.createTime;
        	article+="</span><hr>";
        	$("#content").html(article);
        }
    });
}
/**
 * 侧边栏音乐模块
 */
function getMusic(){
	$.ajax({
		url:"/music/getMusic",
		type: "post",
        async: false,
        success: function(list) {
        	var musicContent="<div class='ibox float-e-margins'><div class='ibox-title'>" +
        			"<h5>音乐盒</h5></div>" +
        			"<div class='ibox-content no-padding' style='overflow:hidden'>"+
        			"<div id='audioPlayer' class='aplayer'></div></div></div>";
        	$("#sidebar").append(musicContent);
        	var ap = new APlayer({
			    element: document.getElementById('audioPlayer'),
			    autoplay: false,
			    showlrc: 1, //false或1	(1:代表true,不能直接写true)
			    mutex: true,
			    preload: 'metadata',
			    theme: '#1AB394',
			    music:list
        	});
        	ap.init();
        }
	})
}
/**
 * 判断是否是手机浏览器打开
 * @returns {Boolean}
 */
function isMobile(){
    var userAgentInfo = navigator.userAgent;
    var Agents = ["Android","iPhone","SymbianOS","Windows Phone","iPad","iPod"];
    var flag = false;
    for (var v = 0; v < Agents.length; v++) {
        if (userAgentInfo.indexOf(Agents[v]) > 0) {
            flag = true;
            break;
        }
    }
    return flag;
}
/**
 * 手机网页的提前准备工作
 */
function mobile(){
	if (isMobile()) {
		$(".slider-tool").remove();
	}
}
/**
 * 侧边小工具，滚动到底部，二维码等
 */
var imgState=false;
$(document).scroll(function(){
	var scroll_H=$(document).scrollTop();
	if (scroll_H>120) {
		$(".slider-tool").fadeIn(500);
		if(imgState){
			$(".qrcodeImg").fadeIn(500);
		}
	}else{
		$(".slider-tool").fadeOut(500);
		$(".qrcodeImg").fadeOut(500);
	}
});
$(".to-top").click(function(){
	$(".qrcodeImg").fadeOut(100);
	imgState=false;
	$("html,body").animate({'scrollTop':0});
});
$(".qrcode").click(function(){
	if (!imgState) {
		if ($(".qrcodeImg").attr("src")!="") {
			$(".qrcodeImg").fadeIn(500);
			imgState=true;
		}else{
			$.ajax({
				url:"/qrcode",
				data:{text:window.location.href,logo:"DullSky"},
				success:function(data){
					if (data.state) {
						$(".qrcodeImg").attr("src",data.imgSrc);
						$(".qrcodeImg").fadeIn(500);
						imgState=true;
					}
				}
			});
		}
	}else{
		$(".qrcodeImg").fadeOut(100);
		imgState=false;
	}
});